import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/business/announcement_item_widget.dart';
import 'package:yysk_app/model/tab_model.dart';
import 'package:yysk_app/pages/nft_pages/home_pages/announcement/announcement_controller.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/empty_widget.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/tab_widget.dart';

class AnnouncementPage extends StatelessWidget {
  static const path = '/announcement';
  const AnnouncementPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.lazyPut<AnnouncementController>(() => AnnouncementController());
    final AnnouncementController controller =
        Get.find<AnnouncementController>();
    final scrollController = controller.scrollController;

    double top = MediaQuery.of(context).padding.top;

    return Scaffold(
      body: Stack(
        children: [
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: ImageAssetWidget(
              width: 375.px,
              height: 812.px,
              imageUrl: 'static/images/bg.png',
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(0.px, top, 0.px, 30.px),
            child: Obx(
              () => Column(
                children: [
                  CustomAppBar2(title: '公告列表', context: context),
                  SizedBox(
                    // color: Colors.amber,
                    width: MediaQuery.of(context).size.width,
                    // color: Colors.amber,
                    child: SingleChildScrollView(
                      scrollDirection: Axis.horizontal,
                      child: Center(
                        child: IntrinsicWidth(
                          child: Container(
                            padding: EdgeInsets.fromLTRB(12.px, 0, 12.px, 0),
                            decoration: BoxDecoration(
                              // color: AppColors.backgroundColorGreenLight
                            ),
                            constraints: BoxConstraints(minWidth: 375.px),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Obx(() {
                                      // final list  = controller.noticeListLabel;
                                      return TabWidget(
                                        activeColor: AppColors.startColor,
                                        isCurrentFontSize: 14,
                                        notCurrentFontSize: 14,

                                        tabColor: AppColors.fontColorGrey,
                                        // crossAxisSpacing: 76,
                                        value: '${controller.currentTab}',
                                        tabList: [
                                          if (controller
                                              .noticeListLabel
                                              .isNotEmpty)
                                            ...controller.noticeListLabel.map((
                                              e,
                                            ) {
                                              return TabModel(
                                                code: '${e.id}',
                                                label: '${e.title}',
                                              );
                                            }),
                                        ],
                                        onChanged: (val) {
                                          controller.changeTab(val);
                                        },
                                      );
                                    }),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: RefreshIndicator(
                      backgroundColor: Colors.transparent,
                      color: Colors.white,
                      onRefresh: controller.handleRefresh,
                      child: _buildPlatformList(
                        controller.noticeList,
                        scrollController,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildPlatformList(
    List<dynamic> list,
    ScrollController scrollController,
  ) {
    return list.isEmpty
        ? const EmptyWidget()
        : ListView.builder(
          controller: scrollController,
          padding: EdgeInsets.only(left: 10.px, right: 10.px),
          itemCount: list.length,
          physics: const AlwaysScrollableScrollPhysics(),
          itemBuilder: (BuildContext context, int index) {
            return AnnouncementItemWidget(item: list[index]);
          },
        );
  }
}
